package com.example.demo.framework.rule;

import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: miaoguoxin
 * @Date: 2020/3/21 20:22
 * @Description:
 */
@Slf4j
public class TraceRoundRobinRule extends RoundRobinRule {

    @Override
    public void initWithNiwsConfig(IClientConfig config) {
    }

    @Override
    public Server choose(Object key) {
        Server server = super.choose(key);
        if (server ==null){
            Instance instance =new Instance();
            instance.setInstanceId("UNKNOWN");
            instance.setPort(80);
            instance.setIp("UNKNOWN");
            server = new NacosServer(instance);
            server.setId("UNKNOWN");
            server.setHost("UNKNOWN");
            log.info("没有找到对应的服务器");
        }
        return server;
    }
}
